from manim import *



class math1(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(60*2+1):       
            dot1=Dot([-500,5*(i-60),0]).set_opacity(0.5)
            dot2=Dot([500,5*(i-60),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(100*2+1):        
            dot3=Dot([(i-100)*5,-300,0]).set_opacity(0.5)
            dot4=Dot([(i-100)*5,300,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])
        #镜头跟进效果 
        self.camera.frame.save_state()
        #题目出现
        text = Tex(r"\text{(张1000)已知微分方程}$y'+y=e^{\sin x},$\text{证明方程存在唯一的以}$2\pi $\text{为周期的解}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,20,0])
        text1 = Tex(r"\text{(2018年数一)已知微分方程}$y'+y=f\left( x\right) ,$\text{其中}$f\left( x\right)  $\text{是R上的连续函数}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,20,0])
        text2 = Tex(r"\text{1.若}$f\left( x\right) =x,$\text{求微分方程的通解}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-23,15,0])
        text3 = Tex(r"\text{2.若}$f\left( x\right) $\text{是周期为T的函数,}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-25,10,0])
        text4 = Tex(r"\text{证明微分方程有通解与其对应,且该通解也为周期函数}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-11,5,0])
        self.play(Write(text1),Write(text2),Write(text3),Write(text4),self.camera.frame.animate.scale(1))
        self.wait()
        #镜头拉回
          #分析文本
        #text100=MarkupText("<b><i>分析/解：</i></b>",color=PINK).scale(5.0).move_to([-38,12.5,0])
        
        self.play(FadeIn(dot1),run_time=0.1)
        self.play(FadeIn(dot2),run_time=0.1)
        self.play(FadeIn(dot3),run_time=0.1)

        #题目向上移动


        text5 = Tex(r"$y=e^{-x}\left[ \int xe^{x}dx+c\right] =\left( x-1\right) +C\cdot e^{-x}$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-11,-5,0])
        self.play(Write(text5))
        text6 = Tex(r"$y=\left( x-1\right) +C\cdot e^{-x}$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text2,RIGHT,buff=1)
        self.play(
            ReplacementTransform(text5,text6)
        )
        self.wait(0.2)
        text7 = Tex(r"\text{3分}",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).move_to([-11,-10,0])
        self.play(Write(text7))
        text8 = Tex(r"\text{3分}",color=PURE_GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text6,RIGHT,buff=1)
        self.play(
            ReplacementTransform(text7,text8)
        )
        self.wait(0.2)

        rectangle=Rectangle(color=BLUE,fill_opacity=0.0,width=38,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle.move_to([-25,10,0])
        self.play(Create(rectangle))

        text9 = Tex(r"$f\left( x+T\right) =f\left( x\right) $",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text3,RIGHT,buff=5)
        self.play(
            ReplacementTransform(rectangle,text9)
        )
        self.wait(0.2)

        rectangle1=Rectangle(color=BLUE,fill_opacity=0.0,width=38,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle1.move_to([-25,5,0])
        self.play(Create(rectangle1))

        text10 = Tex(r"$y=e^{-x}\left[ \int f\left( x\right) e^{x}dx+C\right] $",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-25,-5,0])
        self.play(
            ReplacementTransform(rectangle1,text10)
        )
        self.wait(0.2)

        text11 = Tex(r"$=e^{-x}\left[ \int _{0}^{x}f\left( t\right) e^{t}dt+C\right] $",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text10,RIGHT,buff=1)
        self.play(Create(text11))
        text12 = Tex(r"\text{原则1:不定积分积不出}",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text10,DOWN,buff=5)
        self.play(Create(text12))
        self.play(self.camera.frame.animate.scale(0.5).move_to([-25,-5,0]))
        rectangle2=Rectangle(color=BLUE,fill_opacity=0.0,width=15,height=5
                            ,stroke_color=PURE_GREEN,stroke_width=20)
        rectangle2.move_to([-22.5,-5,0])
        self.play(Create(rectangle2))
        self.play(FadeOut(rectangle2))
        self.play(Restore(self.camera.frame))
        text13 = Tex(r"\text{原则2:需要进行赋值等操作}",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text12,DOWN,buff=2)
        self.play(Create(text13))

        text100 = Tex(r"$y\left( x\right) =e^{-x}\left[ \int _{0}^{x}f\left( t\right) e^{t}dt+C\right] $",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-25,0,0])
        
        rectangle2=Rectangle(color=BLUE,fill_opacity=0.0,width=25,height=5
                            ,stroke_color=PURE_GREEN,stroke_width=20)
        rectangle2.move_to([7,5,0])
        self.play(Create(rectangle2),FadeOut(text13),FadeOut(text12),ReplacementTransform(text10,text100),ReplacementTransform(text11,text100))
        text14 = Tex(r"\text{即证:}$y\left( x+T\right) =y\left( x\right) $",color=PURE_RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text4,RIGHT,buff=2)
        self.play(
            ReplacementTransform(rectangle2,text14)
        )
        self.wait(0.2)
        rectangle3=Rectangle(color=BLUE,fill_opacity=0.0,width=10,height=5
                            ,stroke_color=PURE_GREEN,stroke_width=20)
        rectangle3.move_to([32.5,5,0])
        self.play(Create(rectangle3))
        text15 = Tex(r"$y\left( x+T\right) =e^{-\left( x+T\right) }\left[ \int _{0}^{x+T}f\left( t\right) e^{t-T}dt+C\right]$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-17,-10,0])
        self.play(
            ReplacementTransform(rectangle3,text15)
        )
        self.wait(0.2)
        #下划线
        #下划线
        dot1=Dot([-27,-13,0])
        dot2=Dot([8,-13,0])
        line=Line(dot1,dot2).set_color(PURE_GREEN)
        line.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line))
        text16 = Tex(r"$y\left( x\right) $",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-18,-18,0])
        self.play(Create(text16))
        text17 = Tex(r"$+$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-11,-18,0])
        self.play(Create(text17))
        text18 = Tex(r"$K$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-4,-18,0])
        self.play(Create(text18))

        rectangle4=Rectangle(color=BLUE,fill_opacity=0.0,width=5,height=5
                            ,stroke_color=PURE_GREEN,stroke_width=20)
        rectangle4.move_to([-4,-18,0])
        self.play(Create(rectangle4))
        text19 = Tex(r"$=0$",color=PURE_RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle4,RIGHT,buff=1)
        self.play(Create(text19))
        self.play(FadeOut(text19),FadeOut(rectangle4),FadeOut(text18),FadeOut(text17),FadeOut(text16)
                  ,FadeOut(line))
        rectangle4=Rectangle(color=BLUE,fill_opacity=0.0,width=9,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle4.move_to([-22.5,-10,0])
        rectangle5=Rectangle(color=BLUE,fill_opacity=0.0,width=5,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle5.move_to([-30,0,0])
        text20 = Tex(r"$e^{-\left( x+T\right) }=e^{-x}\cdot e^{-T}$",color=PURE_RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle4,DOWN,buff=2)
        self.play(Create(rectangle4),Create(rectangle5),Create(text20))
        text21 = Tex(r"$y\left( x+T\right) =e^{-x}\left[ \int _{0}^{x+T}f\left( t\right) \cdot e^{t-T}dt+C\cdot e^{-T}\right] $",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-17,-10,0])
        self.play(
            ReplacementTransform(rectangle4,text21)
            ,ReplacementTransform(text20,text21)
            ,FadeOut(rectangle5)
            ,ReplacementTransform(text15,text21)
        )
        self.wait(0.2)
        dot1=Dot([-25,-13,0])
        dot2=Dot([-5,-13,0])
        line=Line(dot1,dot2).set_color(BLUE)
        line.set_stroke(width=30)  # 设置宽度为 30
        dot1=Dot([-26,-3,0])
        dot2=Dot([-15,-3,0])
        line1=Line(dot1,dot2).set_color(BLUE)
        line1.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line),Create(line1))

        text22 = Tex(r"\text{原被积函数}",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-40,-20,0])
        self.play(Create(text22))
        arrow = Arrow(start=[-30,-20,0],end=[-20,-20,0],color=RED
                       , stroke_width=20, tip_length=1)
        text23 = Tex(r"\text{换元}",color=PURE_GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-25,-17,0])
        self.play(GrowArrow(arrow),Create(text23))
        text24 = Tex(r"\text{新被积函数}",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-10,-20,0])
        self.play(Create(text24))




        text220 = Tex(r"\text{原上下限}",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([10,-20,0])
        self.play(Create(text220))
        arrow0 = Arrow(start=[20,-20,0],end=[30,-20,0],color=RED
                       , stroke_width=20, tip_length=1)
        text230 = Tex(r"\text{换元/拆分}",color=PURE_GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([25,-17,0])
        self.play(GrowArrow(arrow0),Create(text230))
        text240 = Tex(r"\text{新上下限}",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([40,-20,0])
        self.play(Create(text240))

        text25 = Tex(r"\text{令}$t-T=u$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text21,RIGHT,buff=5)
        self.play(
            ReplacementTransform(text22,text25)
            ,ReplacementTransform(text23,text25)
            ,ReplacementTransform(text24,text25)
            ,ReplacementTransform(arrow,text25),
            ReplacementTransform(text220,text25)
            ,ReplacementTransform(text230,text25)
            ,ReplacementTransform(text240,text25)
            ,ReplacementTransform(arrow0,text25)
            ,self.camera.frame.animate.scale(1).shift(DOWN*5)
            ,FadeOut(line),FadeOut(line1)
        )
        self.wait(0.2)
        text26 = Tex(r"$y\left( x+T\right) =e^{-x}\left[ \int _{-T}^{x}f\left( u+T\right) e^{u}du+C\cdot e^{-T}\right] $",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-17,-20,0])
        self.play(Create(text26),self.camera.frame.animate.scale(1).shift(DOWN*5))
        #箭头引出
        arrow = Arrow(start=[-25,-27,0],end=[-15,-23,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow))

        text27 = Tex(r"$y\left( x+T\right) =e^{-x}\left[ \int _{-T}^{x}f\left( u\right) e^{u}du+C\cdot e^{-T}\right] $",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-17,-20,0])
        self.play(
            ReplacementTransform(text9,text27)
            ,ReplacementTransform(text26,text27)
            ,ReplacementTransform(arrow,text27)
            ,self.camera.frame.animate.scale(1).shift(DOWN*5)
        )
        self.wait(0.2)
        text28 = Tex(r"$y\left( x+T\right) =e^{-x}\left[ \int _{0}^{x}f\left( u\right) e^{u}du+\int ^{0}_{-T}f\left( u\right) e^{u}du+C\cdot e^{-T}\right] $",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-7,-30,0])
        self.play(Create(text28),self.camera.frame.animate.scale(1).shift(DOWN*5))
        rectangle5=Rectangle(color=BLUE,fill_opacity=0.0,width=5,height=5
                            ,stroke_color=PURE_RED,stroke_width=20)
        rectangle5.move_to([-10,0,0])
        self.play(Create(rectangle5))
        text29 = Tex(r"$y\left( x+T\right) =e^{-x}\left[ \int _{0}^{x}f\left( u\right) e^{u}du+C+\int ^{0}_{-T}f\left( u\right) e^{u}du+C\cdot \left( e^{-T}-1\right) \right] $",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-7,-30,0])
        self.play(
            ReplacementTransform(rectangle5,text29)
            ,ReplacementTransform(text28,text29)
            ,self.camera.frame.animate.scale(1).shift(DOWN*5)
        )
        self.wait(0.2)
        dot1=Dot([-35,-32,0])
        dot2=Dot([15,-32,0])
        line=Line(dot1,dot2).set_color(PURE_RED)
        line.set_stroke(width=30)  # 设置宽度为 30
        text30 = Tex(r"$y\left( x+T\right)=y\left( x\right) +e^{-x}\left[ \int ^{0}_{-T}f\left( u\right) du+c\cdot \left( e^{-T}-1\right) \right] $",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-17,-40,0])
        self.play(Create(text30),self.camera.frame.animate.scale(1).shift(DOWN*5))

        rectangle6=Rectangle(color=BLUE,fill_opacity=0.0,width=40,height=5
                            ,stroke_color=PURE_RED,stroke_width=20)
        rectangle6.move_to([-3,-40,0])
        self.play(Create(rectangle6))
        text31 = Tex(r"$=0$",color=PURE_GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle6,RIGHT,buff=1)
        self.play(Create(text31))
        text32 = Tex(r"$\Rightarrow C=\dfrac{\int ^{0}_{-T}f\left( u\right) e^{u}du}{1-e^{-T}}$",color=PURE_GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text31,RIGHT,buff=1)
        self.play(Create(text32))

        text33 = Tex(r"\text{当}$C=\dfrac{\int ^{0}_{-T}f\left( u\right) e^{u}du}{1-e^{-T}}$\text{时,微分方程有通解与其对应,且该通解也为周期函数}",color=PURE_RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-50,0])
        self.play(Create(text33))
        self.play(self.camera.frame.animate.scale(1.5).move_to([0,-15,0]))
        #箭头引出
        arrow = Arrow(start=[40,-35,0],end=[40,-20,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow))
        text34 = Tex(r"$\Rightarrow C=\dfrac{\int _{0}^{T}f\left( u\right) e^{u}du}{e^{T}-1}$",color=PURE_RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([40,-15,0])
        self.play(Create(text34))

        self.wait()